package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**stest
 */
@SuppressWarnings("serial")
public final class TCSS360GUI extends JFrame
{
  


  /**
   * A default size for the JFrame.
   */
  private static final Dimension DEFAULT_SIZE = new Dimension(600, 300);
  
  private static Map<String, String> user_map = new HashMap<String, String>();
  
  private static final int LOGIN_SIZE = 11;
  
  final JTextField user_name = new JTextField(LOGIN_SIZE);
  final JTextField password = new JTextField(LOGIN_SIZE);
  
  /**
   * A private constructor to inhibit instantiation.
   */
  private TCSS360GUI()
  {
    /* The constructor can set the JFrame title */
    super("CONFERENCE");
  }
  
  /**
   * Displays a simple frame.
   */
  private void start()
  {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setSize(DEFAULT_SIZE);

    setLocationRelativeTo(null);   // centered 
    
    JPanel dialog_panel = new JPanel(new GridLayout(2, 1));
    dialog_panel = inputBox(dialog_panel);
    
    readUsers(); //puts users in map
    
    	
    int result = JOptionPane.showConfirmDialog(null, dialog_panel, 
    			"Please Log-In", JOptionPane.OK_CANCEL_OPTION);
    
    
	if (result == JOptionPane.OK_OPTION) {
		String u_name = user_name.getText();
		
		while(result == JOptionPane.OK_OPTION && (!user_map.containsKey(u_name) 
			  || !user_map.get(u_name).equals(password.getText()))) {
			result = JOptionPane.showConfirmDialog(null, dialog_panel, 
	    			"Invalid Log-In", JOptionPane.OK_CANCEL_OPTION);
			u_name = user_name.getText();
		}
			
		if(result == JOptionPane.OK_OPTION) {
			//pack();
			setVisible(true);
		} else {
			System.out.println("Log-in Cancelled");
			System.exit(1);
		}
	} else {
		System.out.println("Login canceled");
	}
    

	
	

  }

  private static void readUsers() {
	  Scanner s = null;
	  try {
		s = new Scanner(new File("accounts.txt"));
	  } catch (final FileNotFoundException e) {
		System.err.println("File Not Found!");
		e.printStackTrace();
	  }
	  
	  while(s.hasNextLine()) {
		  String line = s.nextLine();
		  
		  final Scanner s2 = new Scanner(line);
		  String user_name= s2.next();
		  String password = s2.next();
		  
		  user_map.put(user_name, password);
		  s2.close();
	  }
	  
  }
  private JPanel inputBox(final JPanel the_panel) {
	 
	  the_panel.add(new JLabel("UserName: "));
	  the_panel.add(user_name);
	
	  the_panel.add(new JLabel("Password: "));
	  the_panel.add(password);
	 
	  return the_panel;
	  
  }


  /**
   * The start point for a program that displays a simple frame.
   * 
   * @param the_args command line arguments - ignored
   */
  public static void main(final String[] the_args)
  {
    /*
     * GUI programs are multi-threaded. We will not have time this quarter to cover the
     * many complex details of multi-threaded programming. You will learn about this when 
     * you take the operating systems class. For now just copy the style
     * of the code shown here wherever you have a main method that is starting a GUI program.
     * If you would like to learn about multi-threaded programming on your own, then read
     * chapter 14 of the Core Java book. An even more thorough and in depth treatment of
     * multi-threaded programming in java is found in the book "Java Concurrency in Practice"
     * by Brian Goetz.  
     * 
     * Execute the GUI in the event dispatch thread
     * (instead of the main thread).
     */
	    EventQueue.invokeLater(new Runnable()
	    {
	      @Override
	      public void run()
	      {
	        new TCSS360GUI().start();
	      } 
	    });
	  }

	}

